package me.hys.api.resource;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import me.hys.api.BaseApi;
import me.hys.base.ApiResult;
import me.hys.base.PageQuery;
import me.hys.bo.resource.CaseBo;
import me.hys.cons.AduitStatus;
import me.hys.cons.ApiStatus;
import me.hys.cons.CaseType;
import me.hys.entity.resource.CaseEntity;
import me.hys.kits.StrKit;
import me.hys.service.resource.ICaseManager;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.google.common.collect.Maps;

/**
 * Author: @author HYS . <br>
 * Date: 2015-9-19 下午3:39:19 . <br>
 * Function: 案例
 */
@Controller
@RequestMapping("/api/case")
public class CaseApi extends BaseApi {
	@Autowired
	private ICaseManager caseManager;

	/**
	 * @Title : detail
	 * @Description : 返回jsp页面详情
	 */
	@RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
	public ModelAndView detail(@PathVariable String id) {
		Map<String, Object> data = Maps.newHashMap();
		CaseEntity entity = caseManager.findById(id);
		data.put("entity", entity);
		if (!StrKit.isBlank(entity.getImages())) {
			String[] images = entity.getImages().split(",");
			data.put("images", images);
		}
		return new ModelAndView("/api/case", data);
	}

	/**
	 * @Title : get
	 * @Description : 查询详情
	 */
	@ResponseBody
	@RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
	public CaseEntity get(@PathVariable String id) {
		return caseManager.findById(id);
	}

	/**
	 * @Title : latest
	 * @Description : 最新一条记录
	 */
	@ResponseBody
	@RequestMapping(value = "/latest", method = RequestMethod.GET)
	public CaseBo latest() {
		return caseManager.findLatest();
	}

	/**
	 * @Title : list
	 * @Description : 查询列表（只显示通过的）
	 */
	@ResponseBody
	@RequestMapping(value = "/list/{category}/{start}", method = RequestMethod.GET)
	public List<CaseBo> list(@PathVariable int category, @PathVariable int start) {
		PageQuery<CaseBo> page = getPageQuery(start);
		page.getCondition().put("category", category);
		page.getCondition().put("status", AduitStatus.PASSED.getStatus());
		PageQuery<CaseBo> result = caseManager.findPageBo(page);
		return result.getList();
	}

	/**
	 * @Title : mylist
	 * @Description : 查询列表（只显示自己的）
	 */
	@ResponseBody
	@RequestMapping(value = "/mylist/{userid}/{start}", method = RequestMethod.GET)
	public List<CaseBo> mylist(HttpServletRequest request,
			@PathVariable String userid, @PathVariable int start) {
		PageQuery<CaseBo> page = getPageQuery(start);
		page.getCondition().put("pubuser", userid);
		String status = request.getParameter("status");
		if (!StrKit.isBlank(status)) {
			page.getCondition().put("status", status);
		}
		PageQuery<CaseBo> result = caseManager.findPageBo(page);
		return result.getList();
	}

	/**
	 * @Title : post
	 * @Description : 上传案例
	 */
	@ResponseBody
	@RequestMapping(value = "/post", method = RequestMethod.POST)
	public ApiResult post(@RequestParam int userid,
			@ModelAttribute CaseEntity entity) {
		entity.setPubuser(userid);
		// 经典案例
		entity.setCategory(CaseType.JDAL.getType());
		caseManager.saveOrUpdate(entity);
		return new ApiResult(ApiStatus.SUCCESS, "操作成功");
	}
}
